package com.cxhc.system.service.cockpit.impl;

import com.cxhc.common.annotation.Integral;
import com.cxhc.common.enums.IntegralType;
import com.cxhc.common.utils.StringUtils;
import com.cxhc.system.domain.app.SysEnterprise;
import com.cxhc.system.mapper.app.CmAreaInfoMapper;
import com.cxhc.system.mapper.app.RpRevHandlingMapper;
import com.cxhc.system.mapper.cockpit.SysEntMapper;
import com.cxhc.system.service.cockpit.ISysEntService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysEntserviceImpl implements ISysEntService {


    @Autowired
    private SysEntMapper sysEntMapper;

    @Autowired
    private CmAreaInfoMapper cmAreaInfoMapper;

    @Autowired
    private RpRevHandlingMapper rpRevHandlingMapper;

    @Override
    public int selectEntByDate(String date,String areaId) {
        return sysEntMapper.countEnt(date,areaId);
    }

    @Override
    public List<String> selectFloorList(Long areaID) {
        ArrayList<String> list = new ArrayList<>();
        for (Integer i = 1; i <= 30; i++) {
            list.add(i.toString()+'F');
        }
//        return sysEntMapper.selectFloorList(areaID);

        return list;
    }

    @Override
    public List<SysEnterprise> selectEnterpriseList(SysEnterprise sysEnterprise) {
        return sysEntMapper.selectSysEnterpriseList(sysEnterprise);
    }


    @Override
    public Map<String, String> selectEntByType(String date,String areaId) {
        //获取企业总数
        BigDecimal sum = new BigDecimal(sysEntMapper.countEnt(date,areaId));
        Map<String, String> mapRes = new HashMap<>();
        List<Map<String, Object>> maps = sysEntMapper.countEntByType(date,areaId);
        for (Map<String, Object> map : maps) {
            BigDecimal typeCount = new BigDecimal(String.valueOf(map.get("typeCount")));
            String industry = (String) map.get("industry");
            if (StringUtils.isEmpty(industry)) {
                industry = "其他";
            }
            if (BigDecimal.ZERO.equals(typeCount)) {
                mapRes.put(industry, "0");
            } else {
                BigDecimal divide = typeCount.divide(sum, 4, RoundingMode.HALF_UP);
                DecimalFormat df = new DecimalFormat("#.##%");
                String format = df.format(divide.doubleValue());
                mapRes.put(industry, typeCount.toString());
            }
        }
        return mapRes;
    }

    @Override
    public Map<String, String> queryEntEvent(String date, String areaId) {
        //获取企业总数
        BigDecimal sum = new BigDecimal(sysEntMapper.countEnt(date,areaId));
        Map<String, String> mapRes = new HashMap<>();
        List<Map<String, Object>> maps = sysEntMapper.queryEntEvent(date,areaId);
        for (Map<String, Object> map : maps) {
            BigDecimal eventCount = new BigDecimal(String.valueOf(map.get("eventCount")));
            String pubEntName = (String) map.get("pubEntName");
            if (StringUtils.isEmpty(pubEntName)) {
                pubEntName = "其他";
            }
            if (BigDecimal.ZERO.equals(eventCount)) {
                mapRes.put(pubEntName, "0");
            } else {
                mapRes.put(pubEntName, eventCount.toString());
            }
        }
        return mapRes;
    }

    @Override
    public Map<String, String> queryEntUser(String date, String areaId) {
        //获取企业总数
        BigDecimal sum = new BigDecimal(sysEntMapper.countEnt(date,areaId));
        Map<String, String> mapRes = new HashMap<>();
        List<Map<String, Object>> maps = sysEntMapper.queryEntUser(date,areaId);
        for (Map<String, Object> map : maps) {
            BigDecimal userCount = new BigDecimal(String.valueOf(map.get("userCount")));
            String entName = (String) map.get("entName");
            if (StringUtils.isEmpty(entName)) {
                entName = "其他";
            }
            if (BigDecimal.ZERO.equals(userCount)) {
                mapRes.put(entName, "0");
            } else {
                mapRes.put(entName, userCount.toString());
            }
        }
        return mapRes;
    }

    @Override
    public Map<String, Map<String, Object>> queryByFloor(Map<String,String> map) {
        Map<String, Map<String, Object>> entMaps = sysEntMapper.countEntByFloor(map);
        Map<String, Map<String, Object>> userMaps = sysEntMapper.countUserByFloor(map);
        Map<String, Map<String, Object>> revMaps = rpRevHandlingMapper.countRevByFloor(map);

        for (Map.Entry<String, Map<String, Object>> stringMapEntry : userMaps.entrySet()) {
            String key = stringMapEntry.getKey();
            Map<String, Object> value = stringMapEntry.getValue();
            Map<String, Object> stringObjectMap = entMaps.get(key);
            if (CollectionUtils.isEmpty(stringObjectMap)){
                entMaps.put(key,value);
            }else {
                stringObjectMap.put("userCount",userMaps.get(key).get("userCount"));
                entMaps.put(key,stringObjectMap);
            }
        }

        for (Map.Entry<String, Map<String, Object>> stringMapEntry : revMaps.entrySet()) {
            String key = stringMapEntry.getKey();
            Map<String, Object> value = stringMapEntry.getValue();
            Map<String, Object> stringObjectMap = entMaps.get(key);
            if (CollectionUtils.isEmpty(stringObjectMap)){
                entMaps.put(key,value);
            }else {
                stringObjectMap.put("revCount",revMaps.get(key).get("revCount"));
                entMaps.put(key,stringObjectMap);
            }
        }

        return entMaps;
    }

    /**
     * 查询企业
     *
     * @param ID 企业主键
     * @return 企业
     */
    @Override
    public SysEnterprise selectSysEnterpriseByID(Long ID) {
        return sysEntMapper.selectSysEnterpriseByID(ID);
    }

    /**
     * 查询企业列表
     *
     * @param sysEnterprise 企业
     * @return 企业
     */
    @Override
    public Map<String, List<SysEnterprise>> selectSysEnterpriseList(SysEnterprise sysEnterprise) {
        List<SysEnterprise> sysEnterprises = sysEntMapper.selectSysEnterpriseList(sysEnterprise);
        return sysEnterprises.stream().collect(Collectors.groupingBy(SysEnterprise::getFloor));
    }

    @Override
    public List<SysEnterprise> selectSysEnterpriseListForCockpit(SysEnterprise sysEnterprise) {
        List<SysEnterprise> sysEnterprises = sysEntMapper.selectSysEnterpriseList(sysEnterprise);
        for (SysEnterprise sysEnterpris : sysEnterprises) {
            String urgentContact = sysEnterpris.getUrgentContact();
            String urgentContactTel = sysEnterpris.getUrgentContactTel();
            if (!StringUtils.isEmpty(urgentContactTel) && urgentContactTel.length() == 11){
                sysEnterpris.setUrgentContactTel(urgentContactTel.substring(0, 3) + "****" + urgentContactTel.substring(7));
            }
            if (!StringUtils.isEmpty(urgentContact) && urgentContact.length() > 1){
                sysEnterpris.setUrgentContact(urgentContact.replace(urgentContact.charAt(1), '*'));
            }
        }
        return sysEnterprises;
    }

    /**
     * 新增企业
     *
     * @param sysEnterprise 企业
     * @return 结果
     */
    @Override
    @Integral(IntegralType.IMPROVE_INFORMATION)
    public int insertSysEnterprise(SysEnterprise sysEnterprise) {
        int i = sysEntMapper.insertSysEnterprise(sysEnterprise);
        cmAreaInfoMapper.updateEntCount(Long.valueOf(sysEnterprise.getAreaID()));
        return i;
    }

    /**
     * 修改企业
     *
     * @param sysEnterprise 企业
     * @return 结果
     */
    @Override
    public int updateSysEnterprise(SysEnterprise sysEnterprise) {
        return sysEntMapper.updateSysEnterprise(sysEnterprise);
    }

    /**
     * 批量删除企业
     *
     * @param IDs 需要删除的企业主键
     * @return 结果
     */
    @Override
    public int deleteSysEnterpriseByIDs(Long[] IDs) {
        return sysEntMapper.deleteSysEnterpriseByIDs(IDs);
    }

    /**
     * 删除企业信息
     *
     * @param ID 企业主键
     * @return 结果
     */
    @Override
    public int deleteSysEnterpriseByID(Long ID, Long areaID) {
        int i = sysEntMapper.deleteSysEnterpriseByID(ID);
        if (i == 1) {
            cmAreaInfoMapper.updateEntCountSub(areaID);
        }
        return i;
    }

    @Override
    public Map<String, String> queryEntEventForArea() {
        //获取企业总数
        Map<String, String> mapRes = new HashMap<>();
        List<Map<String, Object>> maps = sysEntMapper.queryEntEventForArea();
        for (Map<String, Object> map : maps) {
            BigDecimal count = new BigDecimal(String.valueOf(map.get("count")));
            String areaName = (String) map.get("areaName");
            if (StringUtils.isEmpty(areaName)) {
                areaName = "其他";
            }
            if (BigDecimal.ZERO.equals(count)) {
                mapRes.put(areaName, "0");
            } else {
                mapRes.put(areaName, count.toString());
            }
        }
        return mapRes;
    }
}
